# -*- coding: utf-8 -*-
'''
Created on 17.07.2019

@author: yu03
'''
import cv2
import matplotlib.pyplot as plt
from scipy import signal
from FFT_Interpolation import FFT_interpolation_boxcar, FFT_interpolation_2, FFT_interpolation_compare, FFT_cal
import numpy as np


img_set = []
pix_size = 5.3e-6
pattern_path = r'F:\Data_Liang_Yu\Users\yu03\Desktop\3-DoF Interferometer\Experiment Record\Spot_&_Noise\Noise_50fps_180offset_20usExpo.avi'
cap = cv2.VideoCapture(pattern_path)
frame_num = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(frame_num, "Frames")
# while(cap.isOpened()):
for k in range(frame_num):
    ret, frame = cap.read() 
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img_set.append(img)
#     cv2.imshow('frame',img)
#     if cv2.waitKey(1) & 0xFF == ord('q'):
#         break
# cap.release()
# cv2.destroyAllWindows()



# plt.figure(1)
# plt.imshow(img, cmap='gray')
# plt.show()
pix_value_set = []
pix_index = 512, 640
pix_line = img[512]

for img in img_set:
    pix_value = img[pix_index]
    pix_value_set.append(pix_value)
    
# noise_line = np.random.normal(2.43, 0.8, len(img[512]))
noise_line = np.random.normal(2.43, 0.8, len(img[512]))
noise_line = noise_line.round().astype(int)
# noise_line += 2

noise_pix = np.random.normal(2, 0.5, len(img_set))
noise_pix = noise_pix.round().astype(int)
# noise_pix += 2

plt.figure('noise line shape (the last frame)')
plt.subplot(3,1,1)
plt.plot(pix_line, 'b')
plt.plot(noise_line, 'r')
plt.subplot(3,1,2)
plt.hist(pix_line, bins=np.arange(7), color='blue')
y_range = plt.gca().get_ylim()
plt.subplot(3,1,3)
plt.hist(noise_line, bins=np.arange(7), color='red')
plt.ylim(y_range)

plt.figure('1 pixel noise (1000 frames)')
plt.subplot(3,1,1)
plt.plot(pix_value_set, 'b')
plt.plot(noise_pix, 'r')
plt.subplot(3,1,2)
plt.hist(pix_value_set, bins=np.arange(6), color='b')
y_range = plt.gca().get_ylim()
plt.subplot(3,1,3)
plt.hist(noise_pix, bins=np.arange(6), color='r')
plt.ylim(y_range)

plt.show()

